var app = getApp();

Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        wxuser : null,
        order : null,
        sets : null,
        qrcode : null,
        options : null,
        express : [],
        showdeal : false,
        mark : [],
		
    },
    onLoad:function(options){
        var self = this;

        this.data.options = options;
        var postdata = {
            oid : options.id,
            op :'orderinfo',
            v : 1,
        }
        app.com.auth(function(user){
            app.com.http('admin','POST',postdata,0,false,'','',function(res){
                if (!res.data.errno) {
                    self.setData({
                        order : res.data.data.order,
                        good : res.data.data.good,
                        params : res.data.data.params,
                        hexiao : res.data.data.hexiao,
                        psorder : res.data.data.psorder ? res.data.data.psorder : false,
                        mark : res.data.data.mark ? res.data.data.mark : [],
                        orderlist : res.data.data.orderlist ? res.data.data.orderlist : [],
                        inited : true,
                    });

                    if( res.data.data.order.exname && res.data.data.order.exnum ){
                        var postdata = {
                            name : res.data.data.order.exname,
                            num : res.data.data.order.exnum,
                            op:'express'
                        };

                        app.com.http('admin','POST',postdata,0,false,'','',function(res){
                            if (!res.data.errno && res.data) {
                                self.setData({
                                    express : res.data.data
                                })
                            }
                        });
                    }
                }else{
                    app.com.alert(res.data.message);
                }
            });
        });
        wx.setNavigationBarTitle({
          title:  '订单详情',
        });

    },
    onPullDownRefresh: function () {
        this.onLoad(this.data.options);
        wx.stopPullDownRefresh();
    },
    onShareAppMessage : function(){
        return false;
    },
    dealorder( e ){
        var self = this;
        var id = self.data.order.orderid;
        var type = e.currentTarget.dataset.type;
        var postdata = {
            oid : id,
            op:'order'+type,
            isall : e.isall ? 1 : 0,
        };

        if(self.data.order.cartoid && !e.pass) {
            this.setData({
                type : type,
                showdeal : true,
            })
            return false;
        }

        if( type == 'cancel' || type == 'com' || type == 'take' ){
            var nostr = '确定删除订单吗？';
            if( type == 'com' ) {
                var nostr = '确定完成订单吗？';
            }
            if( type == 'take' ) {
                var nostr = '确定接单吗？';
            }
            app.com.confirm(nostr,function(){
                app.com.http('admin','POST',postdata,0,true,'','',function(res){
                    if ( !res.data.errno && res.data.errno !== undefined ) {
                        if( type == 'cancel' ){
                            app.com.toast('已删除');
                            var pages = getCurrentPages();
                            if( pages.length >= 2 ){
                                var prevPage = pages[pages.length - 2];
                                var data = prevPage.data.datalist;
                                for (var i = 0; i < data.length; i++) {
                                    if( data[i].orderid == id ){
                                        data.splice(i,1);
                                        prevPage.setData({
                                            datalist : data,
                                        });
                                    }
                                }
                                wx.navigateBack();
                            }

                        }else if(type == 'com'){
                            app.com.toast('已完成','success',function(){
                                self.setData({
                                    'order.status' : 3
                                });
                            });
                        }else if(type == 'take'){
                            app.com.toast('已接单','success',function(){
                                self.setData({
                                    'order.istaked' : 1
                                });
                            });
                        }

                    }else{
                        app.com.alert(res.data.message);
                    }
                });
            });
        }else if( type == 'pay' || type == 'com' || type == 'express' ){
            app.com.http('order','POST',postdata,0,true,'','',function(res){
                if ( !res.data.errno && res.data.errno !== undefined ) {
                    if( type == 'pay' ){
                        wx.requestPayment({
							'appId' : res.data.data.appId,
                            'timeStamp': res.data.data.timeStamp,
                            'nonceStr': res.data.data.nonceStr,
                            'package': res.data.data.package,
                            'signType': 'MD5',
                            'paySign': res.data.data.paySign,
                            'success': function (payres) {
                                if( payres.errMsg == 'requestPayment:ok' ){
                                    app.util.message('已支付','navigate:/zofui_sitetemp/pages/orderinfo/orderinfo?oid='+id,'success');
                                }
                            },
                            'fail': function (payres) {
                                //支付失败后，
                                console.log( '失败' );
                            }
                        });
                    }
                    if( type == 'express' ) {
                        var arr = res.data.data.data;
                        var step = [];
                        for (var i = 0; i < arr.length; i++) {
                            if( i == 0 ){
                                var item = {current: true,done: true,text: arr[i].time,desc: arr[i].context};
                            }else{
                                var item = {current: false,done: false,text: arr[i].time,desc: arr[i].context};
                            }

                            step.push( item );
                        }
                        self.setData({
                            expressdata : step,
                            showRightPopup: !self.data.showRightPopup,
                            searchfocus : !self.data.searchfocus,
                        });
                    }
                }else{
                    app.com.alert(res.data.message);
                }
            });
        }else if( type == 'toshop' ){
            wx.openLocation({
              latitude:  parseFloat(self.data.sets.shoplat),
              longitude: parseFloat(self.data.sets.shoplng),
              scale: 13
            });
        }else if( type == 'call' ){
            wx.makePhoneCall({
              phoneNumber: self.data.sets.shoptel
            });
        }else if( type == 'send' ){
            self.setData({
                expressoid : id,
                showexpress: true,
                isall : postdata.isall
            });
        }
    },
    subexpress(){ // 发货
        var self = this;
        var postdata = {
            op : 'send',
            oid : self.data.expressoid,
            name : self.data.expressname ? self.data.expressname : '',
            num : self.data.expressnum ? self.data.expressnum : '',
            isall : self.data.isall,
        };
        app.com.http('admin','POST',postdata,0,true,'','',function(res){
            if ( !res.data.errno && res.data.errno !== undefined ) {
                self.setData({
                    'order.status' : 2,
                    showexpress : false,
                });
                app.com.toast(res.data.message);
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    showmark(){
        this.selectComponent('#markorder').showmark();
    },
    canceldeal(){
        this.setData({
            showdeal : false,
        })
    },
    selectdeal(e){
        var tar = {
            currentTarget : {
                dataset : {
                    type : this.data.type,
                }
            },
            isall : false,
            pass : 1,
        }
        if(e.detail.type == 1){
            this.dealorder(tar);
        }else{
            tar.isall = true;
            this.dealorder(tar);
        }
    },
    expressname(e){
        this.setData({
            expressname : e.detail.value,
        });
    },
    expressnum(e){
        this.setData({
            expressnum : e.detail.value,
        });
    },
    toggleexpress(){
        this.setData({
            showexpress: false,
        });
    },
    copy: function (e) {
        wx.setClipboardData({
            data : e.currentTarget.dataset.copy,
            success : function(){}
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showkefuimg : function(e){
        app.com.alert('长按图片识别二维码联系客服',function(){
            wx.previewImage({
                current: e.currentTarget.dataset.img,
                urls: [e.currentTarget.dataset.img]
            });
        });
    },   

})